package service

import (
	"encoding/json"
	"fmt"
	"net/http"
)

type HttpServer struct{}

func hyJudge(w http.ResponseWriter, r *http.Request) {
	Code := r.PostFormValue("Code")

	LuangueType := r.PostFormValue("LuangueType")

	if Code == "" && LuangueType == "" {
		w.WriteHeader(401)
		w.Write([]byte("参数错误"))
	} else {
		res, _ := json.Marshal(CompileObj.Compiles(Code, LuangueType))
		w.WriteHeader(200)
		w.Write(res)
	}

}
func NewHttpServer() HttpServer {
	return HttpServer{}
}

//构建Http服务
func (h HttpServer) StartService(port int) {

	fmt.Println("Http 服务已经启动 Port:", port)

	fmt.Println("接口 : /Api/HyJudge")

	defer fmt.Println("HTTP 服务已经挂掉！")

	http.HandleFunc("/Api/HyJudge", hyJudge)

	http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}
